<?php
/**
 * Object Relational Persistence Mapping Class for table: calendar_event_map
 *
 * @package	com.athena
 * @author	LHV
 */
class CalendarEventMap extends ORDataObject {

	/**#@+
	 * Fields of table: calendar_event_map mapped to class members
	 */
	var $calendar_event_map_id		= '';
	var $calendar_event_id		= '';
	var $person_id		= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'calendar_event_map';

	/**
	 * Primary Key
	 */
	var $_key = 'calendar_event_map_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'CalendarEventMap';

	/**
	 * Handle instantiation
	 */
	function CalendarEventMap() {
		parent::ORDataObject();
	}
	
	function getUsers($eventId){
		$ds = new Datasource_sql();
		$sql = array(
				'cols' 		=> " c.person_id person_id, p.name name, et.name employee_type",
				'from' 		=> " $this->_table c  
				 				 LEFT JOIN person p ON (c.person_id = p.person_id)
				 				 LEFT JOIN employee e ON (c.person_id = e.person_id)
				 				 LEFT JOIN employee_type et ON (e.employee_type_id = et.employee_type_id)",
				 'where'	=> "c.calendar_event_id = $eventId",
				 'ORDER BY' => "name"
				 );
		$ds->setup($this->_db, $sql);
		return $ds->toArray();
	}
	public function addUser($eventId, $userIds){
		$num = count($userIds);
		$ds = new Datasource_sql();
		$sql = "DELETE FROM $this->_table WHERE calendar_event_id = $eventId";
		$this->_execute($sql);
		
		for($i = 0; $i < $num; $i++ ){
			$data = array(
						'calendar_event_id' => $eventId,
						'person_id'			=> $userIds[$i]
					);
			$this->populateArray($data);
			$this->persist();
			$this->calendar_event_map_id = '' ;
		}
		return;
	}
	public function RemoveUser($eventId, $userId){
		$sql = "DELETE FROM $this->_table 
				WHERE calendar_event_id = $eventId AND user_id = $userId ";
		$this->_execute($sql);
		return ;
	}

	
}
?>
